package com.lzx.iteam.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.lzx.iteam.ContactNameCardActivity;
import com.lzx.iteam.R;
import com.lzx.iteam.bean.ChatContact;
import com.lzx.iteam.util.ImageLoaderInterface;
import com.lzx.iteam.util.StringUtil;
import io.rong.imageloader.core.listener.ImageLoadingListener;
import io.rong.imageloader.core.listener.SimpleImageLoadingListener;

import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

/**
 * Created by shijunting on 15-1-22.
 */
public class ChatGroupMemberAdapter extends BaseAdapter {

    private Context mContext;
    private ArrayList<ChatContact> list;
    public int type;
    private ImageLoadingListener animateFirstListener;

    public ChatGroupMemberAdapter(Context context) {
        this.mContext = context;
        list = new ArrayList<ChatContact>();
        animateFirstListener = new AnimateFirstDisplayListener();
    }

    private class AnimateFirstDisplayListener extends
            SimpleImageLoadingListener {

        final List<String> displayedImages = Collections
                .synchronizedList(new LinkedList<String>());

        @Override
        public void onLoadingComplete(String imageUri, View view,
                                      Bitmap loadedImage) {
            if (loadedImage != null) {
                ImageView imageView = (ImageView) view;
                boolean firstDisplay = !displayedImages.contains(imageUri);
                if (firstDisplay) {
                    //FadeInBitmapDisplayer.animate(imageView, 500);
                    displayedImages.add(imageUri);
                }
            }
        }
    }

    public void bindData(ArrayList<ChatContact> list, String name) {
        this.list.clear();
        this.list.addAll(list);
        if (list.size() > 0) {
            ChatContact chatContact = new ChatContact();
            chatContact.userName = name;
            this.list.add(chatContact);
            notifyDataSetChanged();
        }

    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        final ChatContact chatContact = list.get(position);
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.group_grid_item, null);
            holder.img = (ImageView) convertView.findViewById(R.id.iv_grid_manager_img);
            holder.name = (TextView) convertView.findViewById(R.id.tv_grid_manager_name);
            holder.delete = (ImageView) convertView.findViewById(R.id.iv_grid_delete_icon);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.name.setText(list.get(position).getUserName());

        if (("添加".equals(chatContact.getUserName()) || "发起群聊".equals(chatContact.getUserName())) && position == list.size() - 1) {
            holder.img.setImageDrawable(mContext.getResources().getDrawable(R.drawable.selector_manager_add));
        } else {
            String imageUrl = chatContact.getUserImg();
            if (!StringUtil.isEmpty(imageUrl)) {
                ImageLoaderInterface.imageLoader.displayImage(imageUrl, holder.img, ImageLoaderInterface.optionHeadImage);
            }
            holder.img.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if ("0".equals(chatContact.getUserId()) || StringUtil.isEmpty(chatContact.getUserId())) {
                        Toast.makeText(mContext, "对不起，该用户未注册", Toast.LENGTH_LONG).show();
                    } else {
                    Intent intent = new Intent(mContext, ContactNameCardActivity.class);
                    intent.putExtra("user_id", chatContact.getUserId());
                    mContext.startActivity(intent);}
                }
            });
        }
        return convertView;
    }

    private class ViewHolder {
        ImageView img;
        ImageView delete;
        TextView name;
    }

}
